package com.example.demo.controller;

import com.example.demo.pojo.*;
import com.example.demo.service.UService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
//@CrossOrigin(origins="*")
public class UController {
    @Autowired
    private UService uService;
    @GetMapping("/users")
    public Result selectAllu(){
        List<U> us=uService.selectAllu();
        return Result.success(us);
    }
    @PostMapping("/user/{id}")
    public Result getUById(@PathVariable Integer id)
    {
        U u=uService.selectById(id);
        log.info("查询到用户名字，{}",u.getUserName());
        return Result.success(u);
    }
    @PostMapping("/addUser")
    public Result addUser(@RequestBody U u)
    {
        uService.addUser(u);
        log.info("查询到用户头像，{}",u.getImage());
        return Result.success();
    }

    @PostMapping("/ifUser")
    public Result ifUser(@RequestBody U u)
    {
        List<U> us=uService.ifUser(u);
        return Result.success(us);
    }

    @PostMapping("/deleteById")
    public  Result deleteById(@RequestParam String Id)
    {
        uService.deleteById(Id);
        return Result.success();
    }
    @PostMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<String> list)
    {
        uService.deleteByIds(list);
        return Result.success();
    }
    @PostMapping("/userPower/{id}")
    public Result userPower(@PathVariable Integer id)
    {
        List<Power> powers=uService.userPower(id);
        log.info("查询用户{}的权限",id);
        return Result.success(powers);
    }

    @PostMapping("/verify")
    public Result verify(@RequestParam String id,@RequestParam Integer op)
    {
        U us=uService.verify(id,op);
        if(us!=null)
        {
            log.info("查询到用户{}",us);
            return Result.success();
        }

        else
            return Result.error("-1");
    }
    @PostMapping("/upu")
    private  Result UpdateUser(@RequestBody Uup uup)
    {
            uService.upu(uup);
            return Result.success();
    }
}
